package io.yuncheng.common.config;

import com.alibaba.fastjson.JSON;
import io.yuncheng.common.utils.R;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class GenericFilter extends FormAuthenticationFilter {
    private static final Logger log = LoggerFactory.getLogger(GenericFilter.class);

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        if (this.isLoginRequest(request, response)) {
            if (this.isLoginSubmission(request, response)) {
                if (log.isTraceEnabled()) {
                    log.trace("Login submission detected.  Attempting to execute login.");
                }

                return this.executeLogin(request, response);
            } else {
                if (log.isTraceEnabled()) {
                    log.trace("Login page view.");
                }

                return true;
            }
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Attempting to access a path which requires authentication.  Forwarding to the Authentication url [" + this.getLoginUrl() + "]");
            }
          //  this.saveRequestAndRedirectToLogin(request, response);
            response.getWriter().write(JSON.toJSONString(R.error(5001,"Land expiration" +
                    "\n")));
            return false;
        }
    }
}
